package com.wisdytech.linkdcs.pda.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.enums.MalfunctionComfirmEnum;
import com.wisdytech.common.enums.MalfunctionStatusEnum;
import com.wisdytech.linkdcs.common.BaseUtils;
import com.wisdytech.linkdcs.common.exception.ServiceException;
import com.wisdytech.linkdcs.common.handler.BaseService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.IMalfunctionRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.MApplyRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.MalfunctionDemoDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dto.MalfunctionSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MApplyRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MAttachment;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MProcessRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionDemo;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.TPReplaceTemp;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MRegisterVo;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MalfunctionVo;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.pda.service.IMalfunctionService;
import com.wisdytech.linkdcs.system.dao.ISysDictDao;
import com.wisdytech.linkdcs.system.model.SysDict;

@Service
@Transactional
public class MalfunctionServiceImpl extends BaseService<MalfunctionDemoDao, MalfunctionDemo> implements IMalfunctionService {

	@Autowired
	private IMalfunctionRecordDao malfunctionRecordDao;
	
	@Resource
	private MApplyRecordDao mApplyRecordDao;
	
	@Resource
    private ISysDictDao sysDictDao;
	
	
	@Override
	public PageInfo<MalfunctionDemo> listDemoByFilter(String malfunctionType, String malfunctionTitle, Filter filter) {
		/*EntityWrapper<MalfunctionDemo> ew = new EntityWrapper<>();
		if(!malfunctionType.isEmpty()) {
			ew.like("malfunction_Type", malfunctionType);
		}
		if(!malfunctionTitle.isEmpty()) {
			ew.eq("malfunction_Title", malfunctionTitle);
		}
		ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
		List<MalfunctionDemo> list = super.beforeList(MalfunctionDemoDao.class, filter)
                .selectList(ew);
        PageInfo<MalfunctionDemo> page = new PageInfo<>(list);
        return page;*/
        
        List<MalfunctionDemo> list = super.beforeList(MalfunctionDemoDao.class, filter)
                .listDemoByFilter(malfunctionType,malfunctionTitle);
        PageInfo<MalfunctionDemo> page = new PageInfo<>(list);
        return page;
	}

	@Override
	public boolean add(MalfunctionRecord record, String userName) {
		record.setPkId(BaseUtils.getUUID());
		record.setDelFlag(CommonConstants.DEL_FLAG);
		record.setCreateUser(userName);
		record.setCreateDate(new Date());
		record.setUpdateDate(new Date());
		record.setFailureStatus(MalfunctionStatusEnum.register.getCode());
		
		//增加附件表
		MAttachment  attachment=new MAttachment();
		attachment.setAttachmentName(record.getFileName());
		attachment.setAttachmentPath(record.getFilePath());
		attachment.setRecordId(record.getPkId());
		attachment.insert();
		
		
		return record.insert();
	}

	@Override
	public PageInfo<MalfunctionVo> selectAllMalfunctionLists(Filter filter, MalfunctionSearchDto dto) {
		List<MalfunctionVo> result =
				this.beforeList(IMalfunctionRecordDao.class, filter).selectMRecords(dto);
		
		return new PageInfo<>(result);
	}

	@Override
	public MRegisterVo getRecordById(String pkId) {
		return malfunctionRecordDao.selectRegisterDetail(pkId);
	}
	
	
	@Override
	public boolean addApplyRecord(MApplyRecord applyRecord, String username) {
		//更新故障状态
		MalfunctionRecord record=new MalfunctionRecord();
		record.setPkId(applyRecord.getmRecordId());
		record.setFailureStatus("4");//更改为委外状态
		record.updateById();
		//插入委外记录
		applyRecord.setPkId(BaseUtils.getUUID());
		applyRecord.setApplicationDate(new Date());
		applyRecord.setApplicationStatus("0");//待审批状态
		mApplyRecordDao.insert(applyRecord);
		return true;
	}

	@Override
	public boolean addProcessRecord(MProcessRecord pReocrd, String username) {
		//更新记录表
		String recordId =pReocrd.getRecordId();
		MalfunctionRecord record=new MalfunctionRecord();
		record.setPkId(recordId);
		record.setUpdateUser(username);
		record.setUpdateDate(new Date());
		
		if(MalfunctionComfirmEnum.no.getCode().equals(pReocrd.getComfirmFlag())){
			//不需要确认
			record.setFailureResult("1");
			record.setFailureStatus(MalfunctionStatusEnum.ok.getCode());
		}else {
			//待确认
			record.setFailureStatus(MalfunctionStatusEnum.confirm.getCode());
		}
		//更新不成功
		if(!record.updateById()) {
			throw new ServiceException("故障处理更新记录失败");
		}
		
		//插入处理记录
		pReocrd.setPkId(BaseUtils.getUUID());
		
		
		/*//同步更新备件及工装更换
		Filter filter = new Filter();
		filter.setSize(65533);
		PageInfo<TPReplaceTemp> temps =this.iTPReplaceTempService.getTemps(filter, recordId);
		List<TPReplaceTemp> templist = temps.getList();
		for(TPReplaceTemp tPReplaceTemp :templist) {
			tPReplaceTemp.setRecordId(pReocrd.getPkId());
			tPReplaceTemp.setFlag("1");
			this.iTPReplaceTempService.update(tPReplaceTemp);
		}
		//获取设备编码
		MalfunctionRecord mRecord =record.selectById(recordId);
		Equipment  equipment =
				iEqEquipmentDao.selectByEquipCode(mRecord.getEquipCode());
		
		//同步更换信息
		this.savePartReplace(templist,equipment.getPkId());*/
		
		
		return pReocrd.insert();
	}

	@Override
	public List<SysDict> getMalfunctionType() {
		
		EntityWrapper<SysDict> ew =new EntityWrapper<>();
		ew.eq("parent_key", "MALFUNCTION_TYPE");
		/*if(search!=null && search != "") {
			ew.like("DICT_VALUE", search);
		}*/
		return sysDictDao.selectList(ew);
		
		
	}

	@Override
	public List<SysDict> getMalfunctionReason() {
		EntityWrapper<SysDict> ew = new EntityWrapper<>();
		ew.eq("PARENT_KEY", "STOP_REASON");
		/*if(search!=null && search!="") {
			ew.like("DICT_VALUE", search);
		}*/
		return sysDictDao.selectList(ew);
	}

}
